package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-01-18 16:58
 **/
public class LeetCode283 {

    public void moveZeroes(int[] nums) {
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            //i,j初始都指向0,如果nums[i]不为0且i == j,则i,j都右移一位,
            //如果nums[i]不为0且i > j,说明nums[j] = 0,nums[i]!=0,交换两个位置的值,然后i,j同时右移一位
            //如果nums[i] == 0,则只有i右移一位
            if (nums[i] != 0) {
                if (i > j) {
                    nums[j] = nums[i];
                    nums[i] = 0;
                }
                ++j;
            }
        }
    }

}
